package com.yh.service.impl;

import com.yh.config.MyAppConfig;
import com.yh.service.MyWebApplicationService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.net.SocketTimeoutException;

/**
 * @author yuanhao
 * @version 1.0
 * @description
 * @email yuanhao@yinhai.com
 * @date 2022/4/14 11:13
 */
public class MyWebApplicationServiceImpl implements MyWebApplicationService {
    @Override
    public void onStartup(ServletContext servletContext) {
        System.out.println("spring初始化");
        AnnotationConfigWebApplicationContext annotationConfigApplicationContext = new AnnotationConfigWebApplicationContext();
        annotationConfigApplicationContext.register(MyAppConfig.class);
        annotationConfigApplicationContext.refresh();

        DispatcherServlet dispatcherServlet = new DispatcherServlet(annotationConfigApplicationContext);
        ServletRegistration.Dynamic myDispatcherServlet = servletContext.addServlet("MyDispatcherServlet", dispatcherServlet);
        myDispatcherServlet.setLoadOnStartup(1);
        myDispatcherServlet.addMapping("/");
    }
}
